package com.cyt.work.kwmanagement.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.util.EntryUtils;
import com.cyt.work.kwmanagement.dao.KwKcSettingDao;
import com.cyt.work.kwmanagement.model.po.KwKcSetting;
import com.cyt.work.kwmanagement.model.vo.KwKcMessageVo;
import com.cyt.work.kwmanagement.model.vo.KwKcSettingVo;
import com.cyt.work.kwmanagement.model.vo.KwStudentMessageVo;
import com.cyt.work.kwmanagement.service.KwKcMessageService;
import com.cyt.work.kwmanagement.service.KwKcSettingService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.kwmanagement.service.KwStudentMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 考场设置表-考场设置表Service层实现类
 *
 * @author han jun
 * @date 2019-11-26 11:22:26
 */
@Service("kwKcSettingService")
public class KwKcSettingServiceImpl extends BaseServiceImpl<KwKcSetting> implements KwKcSettingService {

    @Resource
    private KwKcSettingDao kwKcSettingDao;


    @Resource
    private KwKcMessageService kwKcMessageService;

    @Resource
    private KwStudentMessageService kwStudentMessageService;

    @Autowired
    public void setBaseDao(KwKcSettingDao kwKcSettingDao) {
        this.baseDao = kwKcSettingDao;
    }

    /**
     * 查询对象集合
     *
     * @param kwKcSettingVo 对象
     * @return List 对象集合
     * @author han jun
     * @date 2019-11-26 11:22:26
     */
    @Override
    public List<KwKcSetting> listExample(KwKcSettingVo kwKcSettingVo) {
        Example example = new Example(KwKcSetting.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        if (!StringUtils.isEmpty(kwKcSettingVo.getApplyId())){
            criteria.andEqualTo("applyId",kwKcSettingVo.getApplyId());
        }
        if (!StringUtils.isEmpty(kwKcSettingVo.getKsId())){
            criteria.andEqualTo("ksId",kwKcSettingVo.getKsId());
        }
        return kwKcSettingDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param kwKcSettingVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:22:26
     */
    @Override
    public ReturnResult page(KwKcSettingVo kwKcSettingVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<KwKcSetting> list = listExample(kwKcSettingVo);
        PageInfo<KwKcSetting> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param kwKcSettingVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:22:26
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(KwKcSettingVo kwKcSettingVo, SysUser sysUser) {
        if (StringUtils.isBlank(kwKcSettingVo.getId())) {
            KwKcSetting kwKcSetting = new KwKcSetting(sysUser);
            kwKcSetting.setApplyId(kwKcSettingVo.getApplyId());
            kwKcSetting.setKsId(kwKcSettingVo.getKsId());
            kwKcSetting.setCjfp(kwKcSettingVo.getCjfp());
            kwKcSetting.setKcsz(kwKcSettingVo.getKcsz());
            kwKcSetting.setKcNum(kwKcSettingVo.getKcNum());
            kwKcSetting.setJkNum(kwKcSettingVo.getJkNum());
            kwKcSetting.setXkNum(kwKcSettingVo.getXkNum());
            kwKcSettingDao.insert(kwKcSetting);
            handleKwKcSetting(kwKcSettingVo.getKsId(),kwKcSettingVo.getApplyId(),kwKcSettingVo.getKwKcMessageJson(),sysUser);
            KwStudentMessageVo kwStudentMessageVo = new KwStudentMessageVo();
            kwStudentMessageVo.setKsId(kwKcSettingVo.getKsId());
            kwStudentMessageVo.setApplyId(kwKcSettingVo.getApplyId());
            kwStudentMessageService.startDistributeStudent(kwStudentMessageVo,sysUser);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            KwKcSetting kwKcSetting = kwKcSettingDao.selectByPrimaryKey(kwKcSettingVo.getId());
            kwKcSetting.setApplyId(kwKcSettingVo.getApplyId());
            kwKcSetting.setKsId(kwKcSettingVo.getKsId());
            kwKcSetting.setCjfp(kwKcSettingVo.getCjfp());
            kwKcSetting.setKcsz(kwKcSettingVo.getKcsz());
            kwKcSetting.setKcNum(kwKcSettingVo.getKcNum());
            kwKcSetting.setUpdateTime(new Date());
            kwKcSetting.setUpdateUser(sysUser.getId());
            kwKcSettingDao.updateByPrimaryKey(kwKcSetting);
            handleKwKcSetting(kwKcSettingVo.getKsId(),kwKcSettingVo.getApplyId(),kwKcSettingVo.getKwKcMessageJson(),sysUser);
            KwStudentMessageVo kwStudentMessageVo = new KwStudentMessageVo();
            kwStudentMessageVo.setKsId(kwKcSettingVo.getKsId());
            kwStudentMessageVo.setApplyId(kwKcSettingVo.getApplyId());
            kwStudentMessageService.startDistributeStudent(kwStudentMessageVo,sysUser);
            return new ReturnResult();
        }
    }

    private void handleKwKcSetting(String ksId,String applyId ,String kwKcMessageJson, SysUser sysUser) {
        KwKcMessageVo kwKcMessageVo = new KwKcMessageVo();
        kwKcMessageVo.setKsId(ksId);
        kwKcMessageVo.setApplyId(applyId);
        kwKcMessageService.removeAll(kwKcMessageVo, sysUser);
        List<KwKcMessageVo> kwKcMessageDTOList = JSON.parseArray(kwKcMessageJson, KwKcMessageVo.class);
       for (int i = 0; i < kwKcMessageDTOList.size(); i++) {
            kwKcMessageDTOList.get(i).setApplyId(applyId);
            kwKcMessageService.addOrUpdate(kwKcMessageDTOList.get(i), sysUser);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param kwKcSettingVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:22:26
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(KwKcSettingVo kwKcSettingVo, SysUser sysUser) {
        //要修改的字段
        KwKcSetting kwKcSetting = new KwKcSetting();
        kwKcSetting.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        kwKcSetting.setUpdateUser(sysUser.getId());
        kwKcSetting.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(KwKcSetting.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwKcSettingVo.getId())) {
            criteria.andIn("id", Arrays.asList(kwKcSettingVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        kwKcSettingDao.updateByExampleSelective(kwKcSetting, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }



    /**
     * 根据条件物理删除
     *
     * @param kwKcSettingVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:22:26
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(KwKcSettingVo kwKcSettingVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(KwKcSetting.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwKcSettingVo.getId())) {
            criteria.andIn("id", Arrays.asList(kwKcSettingVo.getId().split(",")));
        }
        kwKcSettingDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }


    /**
     * 单个对象查询
     *
     * @param kwKcSettingVo 接受参数对象
     * @return KwTeacherMessage
     * @author han jun
     * @date 2019-11-26 11:25:39
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public KwKcSetting findOne(KwKcSettingVo kwKcSettingVo){
        KwKcSetting kwKcSetting = new KwKcSetting();
        if (!StringUtils.isBlank(kwKcSettingVo.getKsId())) {
            kwKcSetting.setKsId(kwKcSettingVo.getKsId());
        }
        if (!StringUtils.isBlank(kwKcSettingVo.getApplyId())) {
            kwKcSetting.setApplyId(kwKcSettingVo.getApplyId());
        }
        //注意单个对象主键id 去找
        List<KwKcSetting> list = listExample(kwKcSettingVo);
        if (list.size()>0){
            return  listExample(kwKcSettingVo).get(0);
        }else {
            return kwKcSetting;
        }

    }

    /**
     * 单个对象Vo查询
     *
     * @param kwKcSettingVo 接受参数对象
     * @return KwTeacherMessage
     * @author han jun
     * @date 2019-11-26 11:25:39
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public KwKcSettingVo findOneVo(KwKcSettingVo kwKcSettingVo){
        List<KwKcSetting> list = listExample(kwKcSettingVo);
        if (list.size()>0){
            KwKcSetting kwKcSetting  = listExample(kwKcSettingVo).get(0);
            KwKcSettingVo kwKcSettingVo1 = new KwKcSettingVo();
            if (kwKcSetting==null){
                return kwKcSettingVo1;
            }else {
                EntryUtils.copyProperties(kwKcSetting, kwKcSettingVo1);
                KwKcMessageVo kwKcMessageVo = new KwKcMessageVo();
                if (StringUtils.isNotBlank(kwKcSettingVo.getApplyId())){
                    kwKcMessageVo.setApplyId(kwKcSettingVo.getApplyId());
                }
                if (StringUtils.isNotBlank(kwKcSettingVo.getKsId())){
                    kwKcMessageVo.setKsId(kwKcSettingVo.getKsId());
                }
                kwKcSettingVo1.setKwKcMessageVOList(kwKcMessageService.listExample(kwKcMessageVo));
                return kwKcSettingVo1;
            }
        }else {
            return kwKcSettingVo;
        }
    }

}